/**
 * 
 * Copyright 2011 Edward A. Webb
 * Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package com.edwardawebb.beatreceipts.cmd;

import java.awt.Frame;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.imageio.ImageIO;

import net.sourceforge.javaocr.gui.meanSquareOCR.TrainingImageSpec;
import net.sourceforge.javaocr.ocrPlugins.mseOCR.CharacterRange;
import net.sourceforge.javaocr.ocrPlugins.mseOCR.OCRScanner;
import net.sourceforge.javaocr.ocrPlugins.mseOCR.TrainingImage;
import net.sourceforge.javaocr.ocrPlugins.mseOCR.TrainingImageLoader;

/**
 * @author Eddie
 *
 */
public class Driver {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		
		OCRScanner scanner = new OCRScanner();

		String trainingImageLoc = "res/images/train/ascii.jpg";
		String targetImageLoc = "res/images/receipts/top.bmp.jpg";
		
        int startChar = '!';
        int endChar = 'Z';

        TrainingImageSpec newImage = new TrainingImageSpec();
        newImage.setFileLocation(trainingImageLoc);
        newImage.setCharRange(new CharacterRange(startChar, endChar));
        try {
        OCRScanner ocrScanner = new OCRScanner();
        HashMap<Character, ArrayList<TrainingImage>> trainingImages = getTrainingImageHashMap(newImage);
        ocrScanner.addTrainingImages(trainingImages);
        BufferedImage targetImage;
		
			targetImage = ImageIO.read(new File(targetImageLoc));
	        String text = ocrScanner.scan(targetImage, 0, 0, 0, 0, null);
	        System.out.println(text);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static HashMap<Character, ArrayList<TrainingImage>> getTrainingImageHashMap(
			TrainingImageSpec imageSpec) throws IOException {
		TrainingImageLoader loader = new TrainingImageLoader();
		HashMap<Character, ArrayList<TrainingImage>> trainingImages = new HashMap<Character, ArrayList<TrainingImage>>();
        Frame frame = new Frame();
		loader.load(
                 frame,
                 imageSpec.getFileLocation(),
                 imageSpec.getCharRange(),
                 trainingImages);
		return trainingImages;
	}

}
